//
//  WBEmoticonBottomView.swift
//  Weibo
//
//  Created by xu on 2017/3/30.
//  Copyright © 2017年 xuantong. All rights reserved.
//

import UIKit

// 记录按钮的枚举
enum WBEmoticonBottomViewType: Int{
    // 最近
    case recent = 2000
    // 默认
    case normal = 2001
    // emoji
    case emoji = 2002
    // 浪小花
    case lxh = 2003
}

class WBEmoticonBottomView: UIStackView {
    // 记录选中的按钮
    var currentButton: UIButton?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
      // 定义一个闭包 事件回调
    var closure: ((WBEmoticonBottomViewType)->())?
    
    
    //MARK: 供外界调用的方法 -> 通过tag获取对应的按钮 设置其选中状态
     func setupCurrentButton(tag: Int){
        let button = viewWithTag(tag+2000)as!UIButton
        //如果当前的按钮和记录的一样 直接返回
        if button == currentButton {
            return
        }
         // 业务逻辑处理 - 单选
        currentButton?.isSelected = false
        button.isSelected = true
        currentButton = button

    }
    // UIStackView 是容器视图
    //MARK: 设置视图
    private func setupUI(){

        self.backgroundColor = UIColor.orange
        //布局方式
        axis = .horizontal
        // 设置填充方shi
        distribution = .fillEqually
        
        addChildButtons(title: "最近", imgName: "left", type: WBEmoticonBottomViewType.recent)
        addChildButtons(title: "默认", imgName: "mid", type: WBEmoticonBottomViewType.normal)
        addChildButtons(title: "Emoji", imgName: "mid", type: WBEmoticonBottomViewType.emoji)
        addChildButtons(title: "浪小花", imgName: "right", type: WBEmoticonBottomViewType.lxh)

        
    }

}


//MARK: 按钮封装
extension WBEmoticonBottomView{

    fileprivate func addChildButtons(title:String,imgName:String,type:WBEmoticonBottomViewType) {
        
        let button =  UIButton()

        button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
        button.tag = type.rawValue
        button.setTitle(title, for: UIControlState.normal)
        button.setBackgroundImage(UIImage(named:"compose_emotion_table_\(imgName)_normal"), for: UIControlState.normal)
         button.setBackgroundImage(UIImage(named:"compose_emotion_table_\(imgName)_selected"), for: UIControlState.selected)
        button.setTitle(title, for: UIControlState.normal)
        button.setTitleColor(UIColor.white, for: UIControlState.normal)
        // 设置默认的为选中的状态

        if type == .normal {
            button.isSelected = true
            currentButton = button
        }
        
        //添加按钮
        addArrangedSubview(button)
    }

    //按钮点击
    @objc fileprivate func buttonClick (button: UIButton){
        // 如果当前的按钮和记录的一样 直接返回
        if button == currentButton {
            return
        }
          // 业务逻辑处理 - 单选
        currentButton?.isSelected = false
        button.isSelected = true
         currentButton = button

         // 执行闭包
      closure?(WBEmoticonBottomViewType(rawValue: button.tag)!)

        
    }

}
